// pages/index/index.js
var app = getApp(); //获取应用实例
const pathLevel = '../../../../'; //路径层级
const quote = require(pathLevel + 'utils/quote.config.js') //统一引入文件
const mta = quote.mta;
const api = quote.api;
const path = quote.paths;


Page({
    data: {
        path: path,
        DailyRecommend: [],
        hideloadmore: true,
        num: 0,
        AlreadyPracticed: app.globalData.AlreadyPracticed, //已练习过的视频对象
    },
    onLoad: function(options) {
        // 生命周期函数--监听页面加载
        var that = this;

        that.GetDailyUpdatesList(0);
    },
    onReady: function() {
        // 生命周期函数--监听页面初次渲染完成
    },
    onShow: function() {
        // 生命周期函数--监听页面显示
        var that = this;
        that.setData({
            AlreadyPracticed: app.globalData.AlreadyPracticed
        })
    },
    onHide: function() {
        // 生命周期函数--监听页面隐藏
    },
    onUnload: function() {
        // 生命周期函数--监听页面卸载
    },
    // 页面相关事件处理函数--监听用户下拉动作
    onPullDownRefresh: function(params) {
        var that = this;

        //数据加载完后停止刷新
        setTimeout(() => {
            console.log('停止刷新');
            wx.stopPullDownRefresh();
        }, 1000);

    },
    onReachBottom: function(e) {
        // 页面上拉触底事件的处理函数
        var that = this;
        console.log('加载更多', e);

        if (!that.data.hideloadmore) {
            console.log('没有更多了')
            return
        }

        wx.showLoading({
            title: '加载中',
        })

        that.setData({
            num: that.data.num + 1
        })

        that.GetDailyUpdatesList(that.data.num, function(res) {
            console.log('上拉加载', res)
            wx.hideLoading();
        });


    },
    onShareAppMessage: function() {
        // 用户点击右上角分享
        return {
            title: 'title', // 分享标题
            desc: 'desc', // 分享描述
            path: 'path' // 分享路径
        }
    },
    tapNavigate: function(e) {
        var that = this;


        // console.log('e', e)
        var qid = e.currentTarget.dataset.qid;
        if (that.data.AlreadyPracticed[qid]) {
            wx.navigateTo({
                url: `/${path.scorePreview}?QID=${qid}`,
                success: function(res) {
                    // success
                }
            })
        } else {
            wx.navigateTo({
                url: `/${path.details}?QID=${qid}`,
                success: function(res) {
                    // success
                }
            })
        }
    },
    GetDailyUpdatesList: function(pageindex, cb) {
        var that = this;

        var dataDic = {
            pageindex: pageindex,
            pagesize: 30
        }
        api.methodGetDailyUpdatesList({
            dataDic: dataDic,
            complete: function(res) {
                console.log(res);
                if (res.data.code != 0) {
                    wx.showModal({
                        title: '提示',
                        content: res.data.msg,
                        showCancel: false,
                        success: function(res) {
                            if (res.confirm) {
                                console.log('用户点击登录')

                            } else if (res.cancel) {
                                console.log('用户点击取消')
                            }
                            return
                        }
                    })
                }

                var DailyUpdatesList = res.data.data.DailyUpdatesList; //获取到的每日更新列表数据
                var DailyRecommend = that.data.DailyRecommend; //data中的每日更新列表数据

                if (DailyUpdatesList == '') {
                    wx.hideLoading();
                    that.setData({
                        hideloadmore: false
                    })
                    return
                }

                console.log(DailyRecommend, DailyUpdatesList)
                that.setData({
                    DailyRecommend: that.InsertData(DailyRecommend, DailyUpdatesList) //DailyRecommend
                });
                typeof cb == "function" && cb(res)
            }
        })
    },
    //将后一个参数里的数据合并到前一个中
    InsertData: function(previous, next) {
        var that = this;

        if (previous.length == 0) {
            previous = next
            return previous;
        }

        if (previous[previous.length - 1]['Moth'] == next[0]['Moth']) {
            previous[previous.length - 1]['DataList'] = previous[previous.length - 1]['DataList'].concat(next[0]['DataList']);
            // previous.push(next.slice(1))
            previous = previous.concat(next.slice(1))
        } else {
            previous = previous.concat(next)
        }


        return previous;
    },
    imgOnLoad(e) {
        var that = this;

        // console.log('图片加载完成', e);
        const obj = e.currentTarget.dataset.obj;
        if (e.detail.height) {
            that.setData({
                [obj]: true
            })
        }
    }
})